package com.exam.online.system.controller;

import com.exam.online.system.model.GetPaperDetailsModel;
import com.exam.online.system.model.QuestionDetilsModel;
import com.exam.online.system.service.PaperService;
import com.exam.online.system.util.ResponseModel;
import com.exam.online.system.util.doc.WordHolder;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.util.List;

import static com.exam.online.system.constant.Constant.SERVICE_PATH_PUBLIC;

/**
 * @author zp
 * @program online-exam-system
 * @description
 * @create 2019-05-05 19:46
 **/
@RestController
@RequestMapping(SERVICE_PATH_PUBLIC)
public class FileController {

    @Autowired
    private PaperService paperService;

    /**
     * 下载试卷word文档
     */
    @GetMapping("file/downWord")
    public String down(@RequestParam("paperId") Integer paperId) throws Exception {
        WordHolder wordHolder = new WordHolder();
        WordHolder.XWPFParagraphBuilder paragraphBuilder = wordHolder.getParagraphBuilder();
        WordHolder.XWPFRunBuilder runBuilder = wordHolder.getRunBuilder();
        XWPFDocument document = new XWPFDocument();
        XWPFParagraph paragraph = paragraphBuilder.init(document).align(ParagraphAlignment.CENTER, TextAlignment.AUTO)
                .initSpacing().spaceInPound(2, 2).build();
        XWPFRun run1 = paragraph.createRun();
        ResponseModel paperDetail = paperService.getPaperDetail(paperId);
        GetPaperDetailsModel data = (GetPaperDetailsModel) paperDetail.getData();
        String name = data.getName();
        //试卷名称（标题）
        runBuilder.init(run1).content(name).font("宋体", "Times New Roman", "30");
        //新增一个段前2倍行距段后3倍行距，文本行距2倍行距的段落
        XWPFParagraph firstPar = paragraphBuilder.init(document).initSpacing().spaceInLine(2, 3)
                .lineSpace(1, null).build();
        //题目
        List<QuestionDetilsModel> questionDetilsModels = data.getQuestionDetilsModels();
        XWPFRun build = null;
        boolean choseTitle = true;
        boolean judgeTitle = true;
        boolean shortTitle = true;
        for (int i = 0; i < questionDetilsModels.size(); i++) {
            QuestionDetilsModel questionDetilsModel = questionDetilsModels.get(i);
            String qustion = "";
            if (1 == questionDetilsModel.getType()) {
                if (choseTitle) {
                    build = runBuilder.init(firstPar).content("一.选择题").samePrOf(run1)
                            .bold(false).font("宋体", "Times New Roman", "16").build();
                    runBuilder.init(firstPar, true).samePrOf(build);
                    choseTitle = false;
                }
                qustion = "(" + (i + 1) + ") " + questionDetilsModel.getTitle() + "(" + questionDetilsModel.getScore() + "分)\t( )";
                build = runBuilder.init(firstPar).content(qustion).samePrOf(run1)
                        .bold(false).font("宋体", "Times New Roman", "16").build();
                String selectA = "  A." + questionDetilsModel.getOptiona();
                String selectB = "  B." + questionDetilsModel.getOptionb();
                String selectC = "  C." + questionDetilsModel.getOptionc();
                String selectD = "  D." + questionDetilsModel.getOptiond();
                runBuilder.init(firstPar, true).samePrOf(build).content(selectA);
                runBuilder.init(firstPar, true).samePrOf(build).content(selectB);
                runBuilder.init(firstPar, true).samePrOf(build).content(selectC);
                runBuilder.init(firstPar, true).samePrOf(build).content(selectD);
                runBuilder.init(firstPar, true).samePrOf(build);
            } else if (2 == questionDetilsModel.getType()) {
                if (judgeTitle) {
                    runBuilder.init(firstPar, true).samePrOf(build);
                    build = runBuilder.init(firstPar).content("二.判断题").samePrOf(run1)
                            .bold(false).font("宋体", "Times New Roman", "16").build();
                    runBuilder.init(firstPar, true).samePrOf(build);
                    judgeTitle = false;
                }
                qustion = "(" + (i + 1) + ") " + questionDetilsModel.getTitle() + "(" + questionDetilsModel.getScore() + "分)\t( )";
                build = runBuilder.init(firstPar).content(qustion).samePrOf(run1)
                        .bold(false).font("宋体", "Times New Roman", "16").build();
                runBuilder.init(firstPar, true).samePrOf(build);
            } else {
                if (shortTitle) {
                    runBuilder.init(firstPar, true).samePrOf(build);
                    build = runBuilder.init(firstPar).content("三.简答题").samePrOf(run1)
                            .bold(false).font("宋体", "Times New Roman", "16").build();
                    runBuilder.init(firstPar, true).samePrOf(build);
                    shortTitle = false;
                }
                qustion = "(" + (i + 1) + ") " + questionDetilsModel.getTitle() + "(" + questionDetilsModel.getScore() + "分)\t( )";
                build = runBuilder.init(firstPar).content(qustion).samePrOf(run1)
                        .bold(false).font("宋体", "Times New Roman", "16").build();
                runBuilder.init(firstPar, true).samePrOf(build);
                runBuilder.init(firstPar, true).samePrOf(build);
                runBuilder.init(firstPar, true).samePrOf(build);
                runBuilder.init(firstPar, true).samePrOf(build);
            }
        }
        if (build == null) {
            return "False";
        }
        String path = ResourceUtils.getURL("/").getPath();
        System.out.println(path);
        wordHolder.saveDocument(document, "D:" + data.getName() + ".doc");
        return "保存位置：D:" + data.getName() + ".doc";
    }
}
